package com.blinddate.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.blinddate.context.BaseContext;
import com.blinddate.pojo.entity.Reach;
import com.blinddate.pojo.entity.ShopInf;
import com.blinddate.pojo.result.Result;
import com.blinddate.pojo.vo.ShopReachVO;
import com.blinddate.service.ReachService;
import com.blinddate.service.ShopInfService;
import com.blinddate.service.UserInfService;
import com.blinddate.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user/reach")
@Api(tags = "到店情况")
@Slf4j
public class ReachController {
    @Autowired
    private ReachService reachService;

    @Autowired
    private UserService userService;

    @Autowired
    private ShopInfService shopInfService;

    @Autowired
    private UserInfService userInfService;


    @PostMapping("/to_shop_record")
    @ApiOperation("到点记录接口")
    public Result<String> toShopRecord(Long shopId){
        //获取用户id
        ShopInf shopInf = shopInfService.getById(shopId);
        if (shopInf==null){
            return Result.error("没有该店面信息");
        }

        Long userId = BaseContext.getCurrentId();
        Integer sex = userInfService.getUserGender(userId);
        if (sex==null){
            return Result.error("没有该用户信息");
        }
        //获取当天时间数据库中的数据
        LambdaQueryWrapper<Reach> reachLambdaQueryWrapper = new LambdaQueryWrapper<>();
        reachLambdaQueryWrapper.eq(Reach::getShopId,shopId);
        //wrapper.ge("LOG_TIME", DateUtils.parseDate(query.getStartDate(), "yyyy/MM/dd"));
        //    wrapper.le("LOG_TIME", DateUtils.parseDate(query.getEndDate() + " 23:59:59"
        reachLambdaQueryWrapper.apply(true, "TO_DAYS(NOW())-TO_DAYS(date_time) = 0");
        Reach reach = reachService.getOne(reachLambdaQueryWrapper);
        boolean shopRecord;
        if (reach!=null){
            reach.setCount(reach.getCount()+1);
            if (sex==1){
                reach.setMale(reach.getMale()+1);
            }else {
                reach.setFemale(reach.getFemale()+1);
            }
            shopRecord = reachService.updateById(reach);
        }else {
            reach = new Reach();
            reach.setCount(1);
            if (sex==1){
                reach.setMale(1);
                reach.setFemale(0);
            }else {
                reach.setMale(0);
                reach.setFemale(1);
            }
            reach.setDateTime(LocalDateTime.now());
            reach.setShopId(shopId);
            shopRecord = reachService.save(reach);
        }

        if (shopRecord){
            return Result.success("记录成功");
        }
        return Result.error("记录失败");
    }

    @GetMapping("/to_shop_count")
    @ApiOperation("获取每天到店记录人数")
    public Result<List<Reach>> toShopCount(@RequestParam Long shopId){
        log.info("获取每天到店记录人数,参数shopId:{}",shopId);
        //判断用户权限
        Boolean decide = userService.identityDecide();
        if (!decide){
            return Result.error("用户权限不够");
        }
        ShopInf shopInf = shopInfService.getById(shopId);
        if (shopInf == null){
            return Result.error("没有该店面信息");
        }
        LambdaQueryWrapper<Reach> reachLambdaQueryWrapper = new LambdaQueryWrapper<>();
        reachLambdaQueryWrapper.eq(Reach::getShopId,shopId);
        List<Reach> list = reachService.list(reachLambdaQueryWrapper);
        return Result.success(list);
    }

    @GetMapping("/to_shop_total")
    @ApiOperation("获取指定天数的到店人数")
    public Result<ShopReachVO> toShopTotal(Long shopId, Integer day){
        log.info("获取指定天数的到店人数,参数shopId:{},day:{}",shopId,day);
        //判断用户权限
        Boolean decide = userService.identityDecide();
        if (!decide){
            return Result.error("用户权限不够");
        }

        if (day==null || day == 0){
            day = reachService.getMorTime(shopId);
        }

        //获取店面名称
        ShopInf shopInf = shopInfService.getById(shopId);
        if (shopInf == null){
            return Result.error("没有该店面信息");
        }
        ShopReachVO shopReachVO = new ShopReachVO();
        String shopInfName = shopInf.getName();
        shopReachVO.setShopName(shopInfName);
        //封装统计数据
        Map<String, Object> shopCount = reachService.getTOShopCount(shopId, day);
        if (shopCount!=null){
            shopReachVO.setCount(shopCount.get("count"));
            shopReachVO.setMale(shopCount.get("male"));
            shopReachVO.setFemale(shopCount.get("female"));
        }else {
            shopReachVO.setCount(0);
            shopReachVO.setMale(0);
            shopReachVO.setFemale(0);
        }
        LocalDateTime nowTime = LocalDateTime.now();
        LocalDateTime endTime = LocalDateTime.now().minusDays(day);
        shopReachVO.setShopId(shopId);
        shopReachVO.setShopStartTime(nowTime);
        shopReachVO.setShopEndTime(endTime);

        return Result.success(shopReachVO);
    }
}
